package cn.iocoder.yudao.module.contract.service.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPackingDO;
import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicPackingMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicPaymentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicPaymentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 付款方式 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicPaymentServiceImpl extends ServiceImpl<BasicPaymentMapper, BasicPaymentDO> implements BasicPaymentService {

    @Resource
    private BasicPaymentMapper basicPaymentMapper;

    @Override
    public Long createBasicPayment(BasicPaymentSaveReqVO createReqVO) {
        // 插入
        BasicPaymentDO basicPayment = BeanUtils.toBean(createReqVO, BasicPaymentDO.class);
        validateUnique(basicPayment);
        basicPaymentMapper.insert(basicPayment);
        // 返回
        return basicPayment.getId();
    }

    @Override
    public void updateBasicPayment(BasicPaymentSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicPaymentExists(updateReqVO.getId());
        // 更新
        BasicPaymentDO updateObj = BeanUtils.toBean(updateReqVO, BasicPaymentDO.class);
        validateUnique(updateObj);
        basicPaymentMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicPayment(Long id) {
        // 校验存在
        validateBasicPaymentExists(id);
        // 删除
        basicPaymentMapper.deleteById(id);
    }

    private void validateBasicPaymentExists(Long id) {
        if (basicPaymentMapper.selectById(id) == null) {
            throw exception(BASIC_PAYMENT_NOT_EXISTS);
        }
    }
    @VisibleForTesting
    void validateUnique(BasicPaymentDO data) {
        long count = basicPaymentMapper.selectCount(new LambdaQueryWrapper<BasicPaymentDO>().eq(BasicPaymentDO::getCode,data.getCode()).ne(data.getId()!=null,BasicPaymentDO::getId,data.getId()));
        // 如果 id 为空，说明不用比较是否为相同 id 的用户
        if (count>0 ) {
            throw exception("付款方式编码已存在");
        }
    }

    @Override
    public BasicPaymentDO getBasicPayment(Long id) {
        return basicPaymentMapper.selectById(id);
    }

    @Override
    public PageResult<BasicPaymentDO> getBasicPaymentPage(BasicPaymentPageReqVO pageReqVO) {
        return basicPaymentMapper.selectPage(pageReqVO);
    }

}